package com.qzsoft.zhjd.server.modules.asphalt.handler;

import java.math.BigDecimal;
import java.math.RoundingMode;

import com.qzsoft.zhjd.server.modules.asphalt.entity.CollectAsphaltDataRatioEntity;
import com.qzsoft.zhjd.server.modules.asphalt.entity.CollectAsphaltDataWeightEntity;

/**
 * @ClassName AsphaltDataRatioHandler
 * @Description 生产数据占比表处理
 * @author linhuawei
 * @since 2022-02-09
 */
public class AsphaltDataRatioHandler {

    /**
     * @Title coverTo
     * @Description 生产数据占比表处理 油石比计算：<br/>
     *              <ul>
     *              <li>内掺法：认为5%的油石比是在整个沥青混合料里，将沥青混合料看成整体1，即油+石=100%</li>
     *              <li>外掺法：认为5%的油石比是在整个沥青混合料里，将集料看成整体1，即油+石=105%<br/>
     *              </li>
     *              </ul>
     * @param weightData 重量数据
     * @return 生产数据占比表数据
     */
    public static CollectAsphaltDataRatioEntity coverTo(CollectAsphaltDataWeightEntity weightData) {

        CollectAsphaltDataRatioEntity ratioData = new CollectAsphaltDataRatioEntity();

        // 处理内掺占比
        BigDecimal totalWeight = weightData.getGravelWeight();
        ratioData.setAsphaltRatioW(countRatio(weightData.getAsphaltAct(), totalWeight));// 外掺油石比
        ratioData.setGravel1RatioW(countRatio(weightData.getGravel1Act(), totalWeight));// 骨料1外掺占比
        ratioData.setGravel2RatioW(countRatio(weightData.getGravel2Act(), totalWeight));// 骨料2外掺占比
        ratioData.setGravel3RatioW(countRatio(weightData.getGravel3Act(), totalWeight));// 骨料3外掺占比
        ratioData.setGravel4RatioW(countRatio(weightData.getGravel4Act(), totalWeight));// 骨料4外掺占比
        ratioData.setGravel5RatioW(countRatio(weightData.getGravel5Act(), totalWeight));// 骨料5外掺占比
        ratioData.setGravel6RatioW(countRatio(weightData.getGravel6Act(), totalWeight));// 骨料6外掺占比
        ratioData.setPowder1RatioW(countRatio(weightData.getPowder1Act(), totalWeight));// 矿粉1外掺占比
        ratioData.setPowder2RatioW(countRatio(weightData.getPowder2Act(), totalWeight));// 矿粉2外掺占比
        ratioData.setPowder3RatioW(countRatio(weightData.getPowder3Act(), totalWeight));// 矿粉3外掺占比
        ratioData.setRecycledMaterialRatioW(countRatio(weightData.getRecycledMaterialAct(), totalWeight));// 再生料外掺占比

        // 内掺法设置物料占比
        totalWeight = weightData.getTotalWeight();
        ratioData.setAsphaltRatioN(countRatio(weightData.getAsphaltAct(), totalWeight));// 内掺油石比
        ratioData.setGravel1RatioN(countRatio(weightData.getGravel1Act(), totalWeight));// 骨料1外掺占比
        ratioData.setGravel2RatioN(countRatio(weightData.getGravel2Act(), totalWeight));// 骨料2外掺占比
        ratioData.setGravel3RatioN(countRatio(weightData.getGravel3Act(), totalWeight));// 骨料3外掺占比
        ratioData.setGravel4RatioN(countRatio(weightData.getGravel4Act(), totalWeight));// 骨料4外掺占比
        ratioData.setGravel5RatioN(countRatio(weightData.getGravel5Act(), totalWeight));// 骨料5外掺占比
        ratioData.setGravel6RatioN(countRatio(weightData.getGravel6Act(), totalWeight));// 骨料6外掺占比
        ratioData.setPowder1RatioN(countRatio(weightData.getPowder1Act(), totalWeight));// 矿粉1外掺占比
        ratioData.setPowder2RatioN(countRatio(weightData.getPowder2Act(), totalWeight));// 矿粉2外掺占比
        ratioData.setPowder3RatioN(countRatio(weightData.getPowder3Act(), totalWeight));// 矿粉3外掺占比
        ratioData.setRecycledMaterialRatioN(countRatio(weightData.getRecycledMaterialAct(), totalWeight));// 再生料外掺占比

        return ratioData;
    }

    /**
     * @param weight 使用量
     * @param total  总量
     * @return 占比
     * @Title countRatio
     * @Description 计算占比
     */
    private static BigDecimal countRatio(BigDecimal weight, BigDecimal total) {
        if (null == weight || null == total || 0 == total.intValue()) {
            return BigDecimal.ZERO;
        }
        return weight.multiply(new BigDecimal(100)).divide(total, 1, RoundingMode.HALF_UP);
    }

}
